package com.mizhiquan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.mizhiquan.common.Const;
import com.mizhiquan.common.ResponseCode;
import com.mizhiquan.common.ServerResponse;
import com.mizhiquan.dao.CircleFocusMapper;
import com.mizhiquan.dao.CircleMapper;
import com.mizhiquan.dao.QuestionMapper;
import com.mizhiquan.dao.UserMapper;
import com.mizhiquan.pojo.Circle;
import com.mizhiquan.pojo.CircleFocus;
import com.mizhiquan.pojo.Question;
import com.mizhiquan.pojo.User;
import com.mizhiquan.service.ICircleService;
import com.mizhiquan.util.DateTimeUtil;
import com.mizhiquan.vo.QuestionListVo;
import com.mizhiquan.vo.UserCirclesVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by SUDDEV on 2017/7/6 0006.
 */
@Service("iCircleService")
public class CircleServiceImpl implements ICircleService {

    @Autowired
    private CircleMapper circleMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CircleFocusMapper circleFocusMapper;

    @Override
    public ServerResponse<Circle> getCircleInfo(String circleId) {
        if (StringUtils.isBlank(circleId)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"参数错误");
        }
        //判断圈子是否存在，存在直接返回圈子信息否则提示没有圈子
        Circle circle = circleMapper.selectByPrimaryKey(circleId);
        if (circle!=null){
            return ServerResponse.createBySuccess(circle);
        }
        return ServerResponse.createByErrorMessage("没有该圈子");
    }

    @Override
    public ServerResponse<PageInfo> getAskList(String circleId, int pageNum, int pageSize, String orderBy) {
        if (StringUtils.isBlank(circleId)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        //判断圈子是否存在，存在直接返回圈子信息否则提示没有圈子
        Circle circle = circleMapper.selectByPrimaryKey(circleId);
        if (circle==null){
            return ServerResponse.createByErrorMessage("没有该圈子");
        }
        //startPage--start 设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        if (StringUtils.isNotBlank(orderBy)){
            // 按热度排序
            if (orderBy.contains("hotspot")){
                // TODO: 2017/7/9 0009 按热度排序
            }
            if (Const.asklistOrderBy.ORDERBY_ASKTIME.contains(orderBy)){
                //设置排序规则，插件会自动将字符串添加到sql语句的orderby 后面
                String[] orderByArray = orderBy.split("%");
                //设置分页排序
                PageHelper.orderBy(orderByArray[0]+" "+orderByArray[1]);
            }
        }
        //填充自己的sql查询逻辑
        List<Question> askList = questionMapper.selectListByCircleId(circleId);
        List<QuestionListVo> questionListVoList = Lists.newArrayList();
        //通过查到的数据进行装配，装配为QuestionListVo
        for (Question question:askList){
            QuestionListVo questionListVo = assmbleQuestionListVo(question);
            questionListVoList.add(questionListVo);
        }
        //根据数据List建立分页
        PageInfo pageInfo = new PageInfo(askList);
        //替换数据为我们处理过后的Vo数据
        pageInfo.setList(questionListVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    //组装QuestionListVo
    private QuestionListVo assmbleQuestionListVo(Question question){
        QuestionListVo questionListVo = new QuestionListVo();
        questionListVo.setQuestionId(question.getQuestionid());
        questionListVo.setQuestionTitle(question.getTitle());
        questionListVo.setQuestionContent(question.getContent());
        questionListVo.setQuestionLikecount(question.getLikecount());
        questionListVo.setQuestionDislikecount(question.getDislikecount());
        questionListVo.setQuestionFocuscount(question.getFocuscount());
        questionListVo.setQuestionAnswercount(question.getAnswercount());
        questionListVo.setQuestionSharecount(question.getSharecount());
        questionListVo.setQuestionFavoritecount(question.getFavoritecount());
        questionListVo.setQuestionAskTime(question.getAskTime());
        questionListVo.setQuestionerId(question.getQuestionerid());
        User user = userMapper.selectByPrimaryKey(question.getQuestionerid());
        questionListVo.setQuestionerNickname(user.getNickname());
        questionListVo.setQuestionerPortrait(user.getPortrait());
        return questionListVo;
    }

    @Override
    public ServerResponse<List<UserCirclesVo>> getUserCircles(String userId){
        List<CircleFocus> circleFocusList = circleFocusMapper.selectCircleFocusList(userId);
        if (CollectionUtils.isEmpty(circleFocusList)){
            return ServerResponse.createBySuccessMessage("用户未关注任何圈子");
        }
        List<UserCirclesVo> circlesVoList = Lists.newArrayList();
        for (CircleFocus circleFocus:circleFocusList){
            UserCirclesVo userCirclesVo = new UserCirclesVo();
            userCirclesVo.setCircleId(circleFocus.getCircleid());
            Circle circle = circleMapper.selectByPrimaryKey(circleFocus.getCircleid());
            userCirclesVo.setCircleName(circle.getCirclename());
            userCirclesVo.setPortrait(circle.getPortrait());
            userCirclesVo.setBackImg(circle.getBackImg());
            userCirclesVo.setFollowerCount(circle.getFocuscount());
            userCirclesVo.setQuestionCount(circle.getQuestioncount());
            userCirclesVo.setDueTime(DateTimeUtil.date2Str(circleFocus.getDueTime()));
            Date nowDate = DateTimeUtil.getNowTime();
            userCirclesVo.setDued(true);
            //如果过期时间大于当前时间，则未过期
            if (circleFocus.getDueTime().getTime() > nowDate.getTime()){
                userCirclesVo.setDued(false);
            }
            circlesVoList.add(userCirclesVo);
        }
        return ServerResponse.createBySuccess(circlesVoList);
    }

    @Override
    public ServerResponse<PageInfo> searchCircles(String keyword,int pageNum,int pageSize){
        //参数错误
        if (StringUtils.isBlank(keyword)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDsc());
        }
        List<Circle> circleList = circleMapper.selectSearchCircleByKeyword(keyword);
        //没有查到匹配结果
        if (CollectionUtils.isEmpty(circleList)){
            return ServerResponse.createByErrorMessage("没有找到匹配圈子");
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo pageInfo = new PageInfo(circleList);
        return ServerResponse.createBySuccess(pageInfo);
    }

}
